<?php
if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_comment.php');
class CommentControl
{
	var $comment_m;
	function __construct()
	{
	
		$this->comment_m = new CommentModule();
	}
	/**
	* ajax改变值
	*
	* @param   int      $ar_id           文章id
	* @param   int      $change_value    值
	* @param   int      $field           要改变的数据字段
	* @return  int
	*/
	function  change_info($comment_id ,$change_value,$field,$type)
	{
		$sql = "UPDATE comment  SET $field = '$change_value' , update_time  = '".$_SERVER['REQUEST_TIME']."',
		update_user  ='".$_SESSION['AName']."',modify_status = 1  WHERE comment_id  = '$comment_id' ";

		return $this->comment_m->change_info($sql);
	
	}
	/**
	* 根据id得到comment信息
	*
	* @param   int         $comment_id
	*
	* @return  array
	*/
	function  get_comment_id_info($comment_id)
	{
		$sql = "SELECT comment_id ,country,sort,is_show ,product_id ,product_name ,content,
		user   FROM `comment` WHERE `comment_id` = '$comment_id' AND `delete`=0";
		return $this->comment_m->get_comment_info($sql);
	}
	/**
	* 根据id得到产品名称
	*
	* @param   int         $product_id
	*
	* @return  array
	*/
	function  get_product_name($product_id)
	{
		$sql = "SELECT `product_name` FROM `product` WHERE `product_id` = '$product_id' AND `product_delete`=0";
		return $this->comment_m->getOne($sql);
	}
	/**
	* 增加或者修改comment
	*
	* @param   string      $type            操作类型
	* @param   int         $comment_id      留言id
	* @param   string      $user     	                用户
	* @param   string      $country         国家
	* @param   int         $sort            排序
	* @param   int         $is_show         是否显示
	* @param   text         $content        内容 
	* @return  int
	*/
	function  operate_comment($type,$comment_id,$user,$country,$sort,$is_show,$content,$product_id = 0,$product_name = '')
	{
		if($type == 'add')
		{
			$sql ="INSERT INTO comment (product_id,product_name,user,country,sort,is_show,content,add_time,update_time,add_user ,update_user ) 
			VALUES ('$product_id','$product_name','$user','$country','$sort','$is_show','$content',UNIX_TIMESTAMP(),UNIX_TIMESTAMP(),'".$_SESSION['AName']."',
			'".$_SESSION['AName']."') ";
		}
		elseif($type == 'edit')
		{
			$sql = " UPDATE comment SET user = '$user',country = '$country',sort = '$sort',is_show = '$is_show',
			content = '$content', update_time = UNIX_TIMESTAMP(),update_user = '".$_SESSION['AName']."', modify_status  
			= 1 WHERE comment_id = '$comment_id' ";
		}
		return $this->comment_m->change_info($sql);	 	
	}
	/**
	* 批量删除标签
	*
	* @param   string         $list_id    id字符串
	*
	* @return  int
	*/
	function delete_comment($list_id)
	{
		$sql = "UPDATE `comment` SET `delete`=1  WHERE `comment_id` IN ($list_id) ";
		
		return $this->comment_m->change_info($sql);
	}

	/**
	* 得到分页相关信息
	*
	* @param   int        $page       页码
	* @param   int        $type       为1代表没有条件查询 为2代表有条件查询
	* @param   int        $comment_type 搜索字段类型（title还是内容）
	* @param   string     $keyword    搜索关键字
	* @return  array
	*/
	function get_url_info($page,$type,$product_id = '',$comment_type = '',$keyword = '')
	{
		$url_str = $where_str = '';
		if($type == 'list')
		{
			
			if(!empty($product_id))
			{
				$url_str = '&product_id = '.$product_id;
				$where_str = ' AND product_id = '.$product_id;
			}
			$sql_p = "SELECT COUNT(*) AS total FROM `comment` WHERE  `delete`=0 $where_str  ";
		}
		$page_info = get_page_info($sql_p,20,$page);
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表comment信息
	*
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   int        $comment_type 搜索字段类型（title还是内容）
	* @param   string     $keyword    搜索关键字
	* @return  array
	*/
	function  get_comment_info($type,$start = 0,$pagesize = 10,$product_id = '',$comment_type = '',$keyword = '')
	{
		$where_str = '';
		if($type == 'list')
		{
			if(!empty($product_id))
			{
				$where_str = ' AND p.product_id = '.$product_id;
			}
			$sql = "SELECT p.product_name,c.comment_id,c.product_id,c.title,c.content,c.user,c.country,c.sort ,c.is_show ,
			IF(c.update_time > 0, FROM_UNIXTIME(c.update_time, '%Y-%m-%d %H:%s'), '') addtime FROM 
			comment c LEFT JOIN product p ON c.product_id = p.product_id WHERE p.product_delete 
			= 0 AND c.delete = 0 $where_str   ORDER BY c.sort  ASC LIMIT $start,$pagesize ";
		}
		//   	elseif($type == 'list_s')
		//   	{
		//   	   $where_str = '';
		//   	   if($product_id) $where_str.=" AND c.`product_id`= '$product_id'";
		//       if(!empty($comment_type))
		//	  	{
		//		  	 if($comment_type == 'title')
		//		  	 {
		//		  	 	$where_str.=" AND `title` LIKE '%".$keyword."%' ";
		//		  	 }
		//		  	 elseif($comment_type == 'content'){
		//		  	 	$where_str.=" AND `content` LIKE '%".$keyword."%' ";
		//		  	 }
		//	  		 elseif($comment_type == 'all'){
		//		  	 	$where_str.=" AND (`title` LIKE '%".$keyword."%' OR `content` LIKE '%".$keyword."%')";
		//		  	 }
		//	  	}
		//	  	
		//	   	 $sql = "
		//	   	  SELECT 
		//	   	 	p.`product_name`,c.`comment_id`,c.`product_id`,c.`title`,c.`content`,c.`user`,
		//	   	 	IF(c.`addtime` > 0, FROM_UNIXTIME(c.`addtime`, '%Y-%m-%d %H:%s'), '') `addtime`
		//	   	  FROM 
		//		   	 `comment` c LEFT JOIN `product` p ON c.`product_id` = p.`product_id`
		//	   	  WHERE 
		//	   	   1=1 $where_str AND p.`product_delete` = 0 AND c.`delete` = 0
		//	   	   ORDER BY c.`comment_id` DESC LIMIT $start,$pagesize ";
		//   	 //die($sql);
		//   	}
		return $this->comment_m->get_comment_info($sql);
	}

}
?>